package com.host.tab;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.widget.ListView;

import com.host.tab.HostTabActivity.RecordAdapter;

public class All {

	private Cursor cursor;
	private Context context;
	
	private static final String IncomingCall = "1";
	private static final String OutgoingCall = "2";
	private static final String MissedCall = "3";

	private static String Incoming_text = "来电";
	private static String Outgoing_text = "去电";
	private static String Missed_text = "未接来电";
	
	public All(Context context){
		this.context = context;
	}
	
	public static List<Map<String, Object>> Display_All(Cursor cursor){
		List<Map<String, Object>> list_all = new ArrayList<Map<String, Object>>();

		String temp_name = null;
		String temp_number = null;
		String temp_type = null;
		String temp_date = null;
		String temp_duration = null;
		String temp_times = null;
		
		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {

			Map<String, Object> listItem = new HashMap<String, Object>();

			temp_type = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
			temp_number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
			temp_name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
			temp_date = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
			temp_duration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));

			// 下面使用的日期类，实现日期的转换，从long类型的时间转换为可读的具体日期。
			SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date date_converted = new Date(Long.parseLong(temp_date));

			// date为转换后的日期字符串
			String date = sfd.format(date_converted);

			listItem.put("date", date);
			listItem.put("duration", temp_duration + "秒");

			if (temp_name == null) {
				temp_name = "未知";
			}

			listItem.put("name", temp_name);
			listItem.put("number", temp_number);

			if (temp_type.equals(IncomingCall)) {
				listItem.put("type", Incoming_text);
			} else if (temp_type.equals(OutgoingCall)) {
				listItem.put("type", Outgoing_text);
			} else if (temp_type.equals(MissedCall)) {
				listItem.put("type", Missed_text);
			} else
				;

			list_all.add(listItem);
			temp_name = null;

			cursor.moveToNext();
		}
		// 过滤后，显示多少个listitem
		// Log.i(TAG, list_all.size() + "");

		return list_all;
		
	}
	
}
